package com.offcn.servlet;

import com.alibaba.fastjson.JSON;
import com.offcn.entity.Course;
import com.offcn.entity.CourseUser;
import com.offcn.entity.ResultBean;
import com.offcn.service.CourseService;
import com.offcn.service.CourseUserService;
import com.offcn.utils.PageTool;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author xyll1
 */
@WebServlet("/courseUser")
public class CourseUserServlet extends BaseServlet {
    CourseUserService courseUserService = new CourseUserService();
    ResultBean resultBean = null;
    CourseService courseService =null;

    private void findAllCourseUser(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String currentPage = request.getParameter("currentPage");
        String pageSize = request.getParameter("pageSize");
        String search = request.getParameter("search");
        List<Object> uids = null;
        if (search != null) {
            uids = courseUserService.selectUidsByName(search);
        }
        int totalCount = courseUserService.selectTotalCount(uids);
        PageTool pageTool = new PageTool(totalCount, currentPage, Integer.parseInt(pageSize));
        List<CourseUser> courseUsers = courseUserService.findAllCourseUser(pageTool, uids);
        pageTool.setList(courseUsers);
        if (courseUsers != null) {
            resultBean = new ResultBean(200, "获取用户成功", pageTool);
        } else {
            resultBean = new ResultBean(500, "获取用户失败", pageTool);
        }
        String s = JSON.toJSONString(resultBean);
        response.getWriter().write(s);
    }
    private void findAllCourses(HttpServletRequest request, HttpServletResponse response) throws IOException {
    courseService    = new CourseService();
        List<Course> courses=courseService.findAllCourses();
        if (courses != null) {
            resultBean = new ResultBean(200, "获取用户成功", courses);
        } else {
            resultBean = new ResultBean(500, "获取用户失败", courses);
        }
        String s = JSON.toJSONString(resultBean);
        response.getWriter().write(s);
    }private void updateCourseUser(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");
        String cid = request.getParameter("cid");
        boolean isSuccess = courseUserService.updateCourseUser(id, cid);
        if (isSuccess) {
            resultBean = new ResultBean(200, "修改成功", true);
        } else {
            resultBean = new ResultBean(500, "修改失败", false);
        }
        String s = JSON.toJSONString(resultBean);
        response.getWriter().write(s);
    }
    //批量删除的方法
    private void deleteAllCourseUser(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String ids = request.getParameter("ids");
        boolean isSuccess = courseUserService.deleteAllCourseUser(ids);
        if (isSuccess) {
            resultBean = new ResultBean(200, "获取用户成功", true);
        } else {
            resultBean = new ResultBean(500, "获取用户失败", false);
        }
        String s = JSON.toJSONString(resultBean);
        response.getWriter().write(s);
    }


}
